/**
 * 
 */
package org.starsource.forum.client.domain;


import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.starsource.forum.client.util.Assertion;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.rpc.IsSerializable;

/**
 * @author james
 *
 */
public class MessageObject implements IsSerializable {

	private int id;
	
	private SessionObject session;
	
	private UserObject owner;
	
	private String content;
	
	private Timestamp createTimestamp;
	
	private Timestamp updateTimestamp;
	
	private List<ResourceObject> resources = new ArrayList<ResourceObject>();
	
	public MessageObject(UserObject owner, String content, SessionObject session, Timestamp createTimestamp, Timestamp updateTimestamp) {
		this.owner = owner;
		this.content = content;
		this.session = session;
		this.createTimestamp = createTimestamp;
		this.updateTimestamp = updateTimestamp;
	}
	
	public MessageObject(int id, UserObject owner, String content, SessionObject session, Timestamp createTimestamp, Timestamp updateTimestamp) {
		this.id = id;
		this.owner = owner;
		this.content = content;
		this.session = session;
		this.createTimestamp = createTimestamp;
		this.updateTimestamp = updateTimestamp;
	}

	public MessageObject() {
	}

	public int getId() {
		return this.id;
	}
	
	public void setId(int id) {
		this.id = id;
	}
	
	public SessionObject getSession() {
		return this.session;
	}
	
	public void setSession(SessionObject session) {
		this.session = session;
	}
	
	public UserObject getOwner() {
		return this.owner;
	}
	
	public void setOwner(UserObject owner) {
		this.owner = owner;
	}

	public String getContent() {
		return this.content;
	}
	
	public void setContent(String content) {
		this.content = content;
	}
	
	public Timestamp getCreateTimestamp() {
		return this.createTimestamp;
	}
	
	public void setCreateTimestamp(Timestamp createTimestamp) {
		this.createTimestamp = createTimestamp;
	}
	
	public Timestamp getUpdateTimestamp() {
		return this.updateTimestamp;
	}
	
	public void setUpdateTimestamp(Timestamp updateTimestamp) {
		this.updateTimestamp = updateTimestamp;
	}
	
	public Date getCreateDate() {
		return new Date(this.createTimestamp.getTime());
	}
	
	public Time getCreateTime() {
		return new Time(this.createTimestamp.getTime());
	}
	
	public String getUpdateTimeString() {
		if (this.updateTimestamp == null) return null;
		
		DateTimeFormat f = DateTimeFormat.getFormat("yyyy-MM-dd HH:mm");  
		return f.format(this.updateTimestamp);
	}
	
	public List<ResourceObject> getResources() {
		return this.resources;
	}
	
	public void setResources(List<ResourceObject> resources) {
		this.resources = resources;
	}
	
	@Override
	public boolean equals(Object obj) {
		try {
			MessageObject object = (MessageObject)obj;
			
			if (this.id != object.id) return false;
			
			if (!Assertion.isEqual(this.session, object.session)) return false;
			
			if (!Assertion.isEqual(this.owner, object.owner)) return false;
			
			if (!Assertion.isEqual(this.content, object.content)) return false;
			
			if (!Assertion.isEqual(this.createTimestamp, object.createTimestamp)) return false;
			
			if (!Assertion.isEqual(this.updateTimestamp, object.updateTimestamp)) return false;
			
			return true;
		} catch (Exception e) {
			return false;
		}
	}

}
